package nc.bs.ic.m4d.cancelsign;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import nc.bs.ic.general.cancelsign.CancelSignBPTemplate;
import nc.bs.ic.general.cancelsign.ICancelSignBP;
import nc.bs.ic.general.cancelsign.ICancelSignRuleProvider;
import nc.bs.ic.general.cancelsign.rule.CancelSignCheckAssetFlag;
import nc.bs.ic.general.cancelsign.rule.CheckHandoverState;
import nc.bs.ic.m4d.base.BPPlugInPoint;
import nc.bs.ic.m4d.base.UpdateSCOnhandRule;
import nc.bs.ic.m4d.cancelsign.rule.AfterCancelSignRuleForLiabilityProcess;
import nc.bs.ic.m4d.cancelsign.rule.CheckCancelSign;
import nc.bs.ic.m4d.cancelsign.rule.PushDeleteIAandTOBills;
import nc.bs.ic.pub.util.SagasUtils;
import nc.bs.scmpub.rule.VOSagaFrozenValidateRule;
import nc.impl.pubapp.pattern.rule.processer.AroundProcesser;
import nc.itf.ic.m4d.compensate.IMaterialOutSagasCompensate;
import nc.vo.ic.m4d.entity.MaterialOutVO;
import nc.vo.scmpub.res.billtype.ICBillType;

public class CancelSignBP implements ICancelSignBP<MaterialOutVO>, ICancelSignRuleProvider<MaterialOutVO> {
	public void addAfterRule(MaterialOutVO[] vos, AroundProcesser<MaterialOutVO> processor) {
		processor.addAfterRule(new UpdateSCOnhandRule(true));
		processor.addAfterRule(new PushDeleteIAandTOBills());
		processor.addAfterRule(new AfterCancelSignRuleForLiabilityProcess());
	}

	public void addBeforeRule(MaterialOutVO[] vos, AroundProcesser<MaterialOutVO> processor) {
		processor.addBeforeRule(new VOSagaFrozenValidateRule<MaterialOutVO>(true));
		processor.addBeforeRule(new CancelSignCheckAssetFlag<MaterialOutVO>());
		processor.addBeforeRule(new CheckHandoverState<MaterialOutVO>());
		
		processor.addBeforeRule(new CheckCancelSign());
	}

	public MaterialOutVO[] cancelSign(MaterialOutVO[] vos) {
		CancelSignBPTemplate<MaterialOutVO> cancelBP = new CancelSignBPTemplate<MaterialOutVO>(BPPlugInPoint.CancelSignBP, this);

		SagasUtils.frozenAndAddSaga(vos, ICBillType.MaterialOut.getCode(), "1", null);
		Map<String, Serializable> paramMap = new HashMap<>();
		paramMap.put("actionname", "cancelsign_4D");
		paramMap.put("hid", vos[0].getParentVO().getCgeneralhid());
		SagasUtils.compensate(paramMap, IMaterialOutSagasCompensate.class);
		return cancelBP.cancelSign(vos);
	}
}